﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using System.Collections.Generic;
using System.Linq;

namespace ChartReversedAxis
{
    public class ReversedAxisLineSeries : LineSeries
    {
        /// <summary>
        /// Updates the visual representation of the data point.
        /// </summary>
        /// <param name="dataPoint">The data point to update.</param>
        protected override void UpdateDataPoint(DataPoint dataPoint)
        {
            // Now the highest value is Minimum
            // Previous code was: double maximum = ActualDependentRangeAxis.GetPlotAreaCoordinate(ActualDependentRangeAxis.Range.Maximum).Value;
            double minimum = ActualDependentRangeAxis.GetPlotAreaCoordinate(ActualDependentRangeAxis.Range.Minimum).Value;
            if (ValueHelper.CanGraph(minimum))
            {
                double x = ActualIndependentAxis.GetPlotAreaCoordinate(dataPoint.ActualIndependentValue).Value;
                double y = ActualDependentRangeAxis.GetPlotAreaCoordinate(dataPoint.ActualDependentValue).Value;

                if (ValueHelper.CanGraph(x) && ValueHelper.CanGraph(y))
                {
                    dataPoint.Visibility = Visibility.Visible;

                    double coordinateY = Math.Round(minimum - (y + (dataPoint.ActualHeight / 2)));
                    Canvas.SetTop(dataPoint, coordinateY);
                    double coordinateX = Math.Round(x - (dataPoint.ActualWidth / 2));
                    Canvas.SetLeft(dataPoint, coordinateX);
                }
                else
                {
                    dataPoint.Visibility = Visibility.Collapsed;
                }
            }

            if (!UpdatingDataPoints)
            {
                UpdateShape();
            }
        }

        /// <summary>
        /// Updates the Series shape object from a collection of Points.
        /// </summary>
        /// <param name="points">Collection of Points.</param>
        protected override void UpdateShapeFromPoints(IEnumerable<Point> points)
        {
            if (points.Any())
            {
                // I can't change the UpdateShape method, so I'll fix the maximum and mimimum values here
                double maximum = ActualDependentRangeAxis.GetPlotAreaCoordinate(ActualDependentRangeAxis.Range.Maximum).Value;
                double minimum = ActualDependentRangeAxis.GetPlotAreaCoordinate(ActualDependentRangeAxis.Range.Minimum).Value;

                PointCollection pointCollection = new PointCollection();
                foreach (Point point in points)
                {
                    pointCollection.Add(new Point(point.X, minimum - (maximum - point.Y)));
                }
                SetValue(PointsProperty, pointCollection);
            }
            else
            {
                SetValue(PointsProperty, null);
            }
        }
    }
}
